package com.xwyzsn.system.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletResponse;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.apache.commons.codec.binary.Base64;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xwyzsn.common.annotation.Log;
import com.xwyzsn.common.core.controller.BaseController;
import com.xwyzsn.common.core.domain.AjaxResult;
import com.xwyzsn.common.enums.BusinessType;
import com.xwyzsn.system.domain.Borrow;
import com.xwyzsn.system.service.IBorrowService;
import com.xwyzsn.common.utils.poi.ExcelUtil;
import com.xwyzsn.common.core.page.TableDataInfo;

/**
 * 设备借还Controller
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/system/borrow")
public class BorrowController extends BaseController
{
    @Autowired
    private IBorrowService borrowService;

    /**
     * 查询设备借还列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Borrow borrow)
    {
        startPage();
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        return getDataTable(list);
    }

    /**
     * 导出设备借还列表
     */
    @Log(title = "设备借还", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Borrow borrow)
    {
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        ExcelUtil<Borrow> util = new ExcelUtil<Borrow>(Borrow.class);
        util.exportExcel(response, list, "设备借还数据");
    }

    /**
     * 获取设备借还详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(borrowService.selectBorrowById(id));
    }

    /**
     * 新增设备借还
     */
    @Log(title = "设备借还", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Borrow borrow)
    {
        return toAjax(borrowService.insertBorrow(borrow));
    }

    /**
     * 修改设备借还
     */
    @Log(title = "设备借还", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Borrow borrow)
    {
        return toAjax(borrowService.updateBorrow(borrow));
    }

    /**
     * 删除设备借还
     */
    @Log(title = "设备借还", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(borrowService.deleteBorrowByIds(ids));
    }

    @GetMapping("/test_notice")
    public AjaxResult testNotice() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, ApiException {
        String CUSTOM_ROBOT_TOKEN = "ding7yqzizsypgoqwym2";
        String SECRET = "1MU3qXpPGyCrj1TYCOZUBwuavdEZv06j3tGNLO0qCejUef_uUWyp0pHsHg8gXt89";
        String signData = "SEC60123cbec46484ed33aa8f70e9a089e14ef2240e9ab09b1a4bfc24e99b67c121";
        try {
            Long timestamp = System.currentTimeMillis();
            System.out.println(timestamp);
            String secret = SECRET;
//            String stringToSign = timestamp + "\n" + secret;
//            Mac mac = Mac.getInstance("HmacSHA256");
//            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
//            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
//            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
//            System.out.println(sign);

            //sign字段和timestamp字段必须拼接到请求URL上，否则会出现 310000 的错误信息
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=01d2db2f6e76ba02ed2c785f7fe11a51aa6fa444117df75d8c10526d613a1481&sign="+signData+"&timestamp="+timestamp);
            OapiRobotSendRequest req = new OapiRobotSendRequest();
            /**
             * 发送文本消息
             */
            //定义文本内容
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("钉钉，让进步发生");
            //定义 @ 对象
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//            at.setAtUserIds(Arrays.asList(USER_ID));
            //设置消息类型
            req.setMsgtype("text");
            req.setText(text);
            req.setAt(at);
            OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }


        return AjaxResult.success();
    }
}
